package com.zx.system.block.mapstruct;


import com.zx.core.context.TenantContextHolder;
import com.zx.core.enums.StatusEnum;
import com.zx.feign.client.dto.DictTypeDTO;
import com.zx.system.block.model.SysDictType;
import com.zx.system.block.param.DictTypeParam;
import com.zx.system.block.vo.SysDictTypeVO;
import org.mapstruct.*;
import org.mapstruct.factory.Mappers;

/**
 * <p>
 * description: 字典类型参数映射 <br>
 * create: 2025-11-18
 * </p>
 *
 * @author zhou xun
 */
@Mapper(unmappedTargetPolicy = ReportingPolicy.IGNORE)
public interface DictTypeMapStructMapper {
    DictTypeMapStructMapper INSTANCE = Mappers.getMapper(DictTypeMapStructMapper.class);

    /**
     * 将DictTypeParam转换为SysDictType实体
     *
     * @param dictTypeParam 字典类型参数
     * @return SysDictType实体
     */
    SysDictType dictTypeParamToSysDictType(DictTypeParam dictTypeParam);

    /**
     * 将SysDictType实体转换为SysDictTypeVO视图对象
     *
     * @param sysDictType 字典类型实体
     * @return SysDictTypeVO视图对象
     */
    SysDictTypeVO sysDictTypeToSysDictTypeVO(SysDictType sysDictType);

    /**
     * 在映射完成后，设置描述性字段
     *
     * @param sysDictType   字典类型实体
     * @param sysDictTypeVO 字典类型VO
     */
    @AfterMapping
    default void setDescriptions(SysDictType sysDictType, @MappingTarget SysDictTypeVO sysDictTypeVO) {
        // 设置状态描述
        Integer status = sysDictType.getStatus();
        StatusEnum statusEnum = StatusEnum.getEnumByCode(status);
        if (statusEnum != null) {
            sysDictTypeVO.setStatusDesc(statusEnum.getDesc());
        }
    }

    /**
     * 在映射完成后，设置租户名
     *
     * @param sysDictType   字典类型实体
     * @param sysDictTypeVO 字典类型VO
     */
    @AfterMapping
    default void setTenantName(SysDictType sysDictType, @MappingTarget SysDictTypeVO sysDictTypeVO) {
        // 设置租户名
        sysDictTypeVO.setTenantName(TenantContextHolder.getTenantName());
    }


    /**
     * 将SysDictType实体转换为DictTypeDTO视图对象
     *
     * @param sysDictType 字典类型实体
     * @return DictTypeDTO视图对象
     */
    @Mapping(target = "statusDesc", source = "status", qualifiedByName = "getStatusDesc")
// 解决record 不可变不能使用后置处理
    DictTypeDTO sysDictTypeToDictTypeDTO(SysDictType sysDictType);

    @Named("getStatusDesc")
    default String getStatusDesc(Integer status) {
        StatusEnum statusEnum = StatusEnum.getEnumByCode(status);
        return statusEnum != null ? statusEnum.getDesc() : null;
    }

}